/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.mybeans.dao.DAOException;
import org.mybeans.forms.FormBeanFactory;

import scheduler.formbean.EditBookmarkForm;
import scheduler.model.BookmarkDao;
import scheduler.model.Model;
import scheduler.model.UserDao;


public class EditAction extends Action {
	private FormBeanFactory<EditBookmarkForm> formBeanFactory = FormBeanFactory.getInstance(EditBookmarkForm.class,"<>\"");

	private BookmarkDao bookmarkDao;

	private UserDao userDao;

    public EditAction(Model model) {
    	bookmarkDao = model.getBookmarkDao();
    	userDao = model.getUserDao();
	}

    public String getName() { return "edit-bookmark.do"; }

    public String perform(HttpServletRequest request) {
    	EditBookmarkForm form = formBeanFactory.create(request);
    	
    	// Set up the request attributes (the errors list and the form bean so
        // we can just return to the jsp with the form if the request isn't correct)
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        try {
			request.setAttribute("registeredUsers", userDao.getAll());
		} catch (DAOException e) {
    		errors.add(e.getMessage());
    		return "error.jsp";
		}
    	

        
        HttpSession session = request.getSession();

		try {
			int id = form.getId();
			session.setAttribute("bookmark", bookmarkDao.lookup(id));
			
	        return "modify.jsp";
		} catch (DAOException e) {
    		errors.add(e.getMessage());
    		return "error.jsp";
    	}
    }
}
